package main

import (
	"fmt"
	"math"
)

func main() {
	ifDemo()
	var abc = switchDemo("+", 3, 6)
	println(abc)
	//数组
	var arr = [5]int{100, 23, 95, 49, 20}
	println(getAverage(arr, 5))
	println(3 / 100)
	var value float32 = 1000
	println(value / 3)
	pointerDemo()

	//函数值传递
	var x, y int16 = 12, 16
	swap(x, y)
	println(x, y)
	var xx, yy int16 = swap(13, 15)
	println(xx, yy)
	a, b := swapStr("baidu", "google")
	println(a, b)

	var aa, bb int = 11, 22
	swapPointer(&aa, &bb)
	println(aa, bb)

	//匿名函数及函数作为另一个函数的实参
	getSqrtValue := func(x float64) float64 {
		return math.Sqrt(x)
	}
	fmt.Println(getSqrtValue(8))

	//闭包
	/* nextNumber 为一个函数，函数 i 为 0 */
	nextNumber := getSequence()

	/* 调用 nextNumber 函数，i 变量自增 1 并返回 */
	fmt.Println(nextNumber())
	fmt.Println(nextNumber())
	fmt.Println(nextNumber())

	/* 创建新的函数 nextNumber1，并查看结果 */
	nextNumber1 := getSequence()
	fmt.Println(nextNumber1())
	fmt.Println(nextNumber1())

	//结构体
	var c1 Circle
	c1.radius = 10
	fmt.Println("圆的面积为：", c1.getArea())

	//指针数组
	pointerArrayDemo()
	//指向指针的指针数组
	pointerArrayDemo_01()
}

/**
 * if 示例
 */
func ifDemo() {
	var a int = 10

	if a < 20 {
		fmt.Println("if ")
	}
}

func switchDemo(operator string, a, b int16) int16 {

	switch {
	case operator == "+":
		return a + b
	case operator == "-":
		return a - b
	case operator == "/":
		return a / b
	case operator == "*":
		return a * b
	case operator == "%":
		return a % b
	default:
		return a + b
	}

	/**
	switch operator {
	case "+":
		return a + b
	case "-":
		return a - b
	case "/":
		return a / b
	case "*":
		return a * b
	case "%":
		return a % b
	default:
		return a + b
	}
	*/
}

/**
 *  数组求平均值
 */
func getAverage(arr [5]int, size int) float32 {
	sum := 0
	for i := 0; i < size; i++ {
		sum += arr[i]
	}

	return float32(sum) / float32(size)
}

/**
 * 指针变量
 */
func pointerDemo() {
	var a int = 30 //定义实际变量
	var ip *int    //声明指针变量

	ip = &a //指针变量存储地址

	fmt.Printf("a变量的存储地址：%x \n", &a)
	fmt.Printf("指针变量ip地址：%x \n", ip)
	fmt.Printf("指针变量值：%d \n", *ip)
}

/**
 * 值传递
 */
func swap(x, y int16) (int16, int16) {
	return y, x
}

func swapStr(x, y string) (string, string) {
	return y, x
}

/**
 * 引用传递
 */
func swapPointer(x *int, y *int) {
	var temp int
	temp = *x
	*x = *y
	*y = temp
}

/**
 * 闭包 函数作为返回值
 */
func getSequence() func() int {
	i := 0
	return func() int {
		i += 1
		return i
	}
}

/**
 * 结构体
 */
type Circle struct {
	radius float64
}

func (c Circle) getArea() float64 {
	//c.radius 即为 Circle 类型对象中的属性
	return 3.14 * c.radius * c.radius
}

const MAX int = 3

/**
 * 指针数组
 */
func pointerArrayDemo() {
	arr := [3]string{"java", "python", "go"}
	var ptr [MAX]*string
	for i := 0; i < MAX; i++ {
		ptr[i] = &arr[i] //将地址赋值给指针数组
	}
	for j := 0; j < MAX; j++ {
		fmt.Printf("ptr[%d]的值为：%s \n", j, *ptr[j])
	}

}

/**
 * 指向指针的指针数组
 */
func pointerArrayDemo_01() {
	arr := []string{"java", "python", "go"}
	var ptr [MAX]*string
	var pptr [MAX]**string
	for i := 0; i < MAX; i++ {
		ptr[i] = &arr[i] //将地址赋值给指针数组
	}
	for j := 0; j < MAX; j++ {
		fmt.Printf("ptr[%d]的值为：%s \n", j, *ptr[j])
	}
	for i := 0; i < MAX; i++ {
		pptr[i] = &ptr[i] //将地址赋值给指针数组
	}
	for j := 0; j < MAX; j++ {
		fmt.Printf("pptr[%d]的值为：%s \n", j, **pptr[j])
	}
}
